<?php


namespace app\admin\controller\Web;

use app\admin\model\WebCate;
use app\admin\model\WebChapter;
use app\admin\model\WebUser;
use app\admincommon\controller\AdminController;
use think\facade\View;

class Artical extends AdminController{

    protected $chapters;
    protected $webcate;
    protected $webuser;

    public function __construct()
    {
        $this->chapters = new WebChapter();
        $this->webcate = new WebCate();
        $this->webuser = new WebUser();
         
    }


    function index(){

        $page = input("page") ? input("page")-1 : 0;
        $pagesize=10;

        $data = $this->chapters->order("id desc")->limit($page*$pagesize,$pagesize)->select();
        $total=$this->chapters->count();

        if(!empty($data)){

            for($i=0;$i<count($data);$i++){
                $cate = $this->webcate->find($data[$i]["cateid"]);
                if(!empty($cate)){
                    $data[$i]["category_name"] = $cate->category_name;
                }else{
                    $data[$i]["category_name"]="未知";
                }

                if($cate){
                    $data[$i]["cover"]= $data[$i]["cover"] ? $data[$i]["cover"] : setdefaultimage($cate->alias);
                }
                $data[$i]["keywords_short"] = mb_substr($data[$i]["keywords"], 0 , 8);
                $data[$i]["descriptions_short"] = mb_substr($data[$i]["descriptions"], 0 , 8);
            }

        }

        View::assign([
            "chapters_list"=>$data,
            "total"=>$total,
            "page"=>input("page")
        ]);

        return View::fetch();

    }


    function add(){

        if(request()->isPost()){

            $admin = session("admin");

            $post = request()->post();

            $time=time();

            $data["cateid"]=$post["cateid"];
            $data["title"]=$post["title"];
            $data["status"]=$post["status"];
            $data["content"]=htmlspecialchars($post["html"]);
            $data["userid"]=$admin["id"];

            $data["tags"]=$post["tags"];
            $data["keywords"]=$post["keywords"];
            $data["descriptions"]=$post["descriptions"];
            $data["postuid"]=$post["postuid"];
            $data["cover"]=$post["cover"];
            $data["lang"]=$post["lang"];

            $data["create_time"]=$time;
            $data["update_time"]=$time;

            try {

                $id = $this->chapters->insertGetId($data);
            
            } catch (\Exception $e) {

                $this->error('保存失败:try catch');

            }

            $id ? $this->result(["id"=>$id],1,"保存成功","json") : $this->error('保存失败');

        }

        $category = $this->webcate->select();

        $users = $this->webuser->select();

        View:: assign([
            "category"=> $category,
            "users"=>$users
        ]);

        return View::fetch();
        
    }


    function edit(){

        $id = input("id");

        $data = $this->chapters->find($id);

        empty($data) && $this->error("文章不存在");

        if(request()->isPost()){

            $admin = session("admin");
            $post = request()->post();

            $updata["id"]=$post["id"];
            $updata["cateid"]=$post["cateid"];
            $updata["title"]=$post["title"];
            $updata["status"]=$post["status"];
            $updata["content"]=htmlspecialchars($post["html"]);
            $updata["userid"]=$admin["id"];
            $updata["tags"]=$post["tags"];
            $updata["postuid"]=$post["postuid"];
            $updata["cover"]=$post["cover"];
            $updata["lang"]=$post["lang"];

            $updata["keywords"]=$post["keywords"];
            $updata["descriptions"]=$post["descriptions"];
            $updata["update_time"]=time();

            try {

                $res = $this->chapters->update($updata);
            
            } catch (\Exception $e) {

                $this->error('保存失败:try catch'.$e);

            }

            $res ? $this->result(["id"=>$id],1,"更新成功","json") : $this->error('保存失败');


        }

        $category = $this->webcate->select();
        $users = $this->webuser->select();

        $data["content"]=htmlspecialchars_decode($data["content"]);
        View::assign([
            "data"=>$data,
            "category"=> $category,
            "users"=>$users
    
        ]);
        return View::fetch();

    }










}